<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class event_api
{
	public function __construct() {
	}


	public function __call($func_name, $func_args){
		$event_arr = array('daylogin','setavatar','postshare','postvideo',
				'postcomment','realemail','addlike','beenlike','digist',
				'top','follow','beenfollow','register','addlikealbum','afterpostshare',
				'deleteAttach','aftereditshare','cropimagestart','cropimageend');
		if(!in_array($func_name, $event_arr)) return;

		$allowplugin_evts = array('postshare','postvideo','afterpostshare','deleteAttach','aftereditshare',
				'cropimagestart','cropimageend');
		if(in_array($func_name, $allowplugin_evts)) {
			$this->plugin_process($func_name,$func_args);
		}
		
		
		$needprocess_arr = array('register','addlike','postcomment','setavatar','postshare'
				,'postvideo','follow','addlikealbum');
		if(in_array($func_name, $needprocess_arr)&&method_exists($this,$func_name.'_p')) {
			call_user_func_array(array($this, $func_name.'_p'), $func_args);
		}

		$logdb = load_db('event');
		$ruledb = load_db('event/eventrule');
		$rule = $ruledb->getEventByCode($func_name);
		if($rule){
			$userid = $func_args[0];
			$log = $logdb->getEventByUserAndRule($userid,$rule['event_id']);
			//onetime 只一次 notimelimit 限定次数 everyday以每天为限
			if($rule['cycletype']=='onetime'&&$log['total']==0){
				$data['dateline']=TIMESTAMP;
				$data['cyclenum']=1;
				$data['total']=$log['total']+1;
				$data['ext_credits_1']=$rule['ext_credits_1'];
				$data['ext_credits_2']=$rule['ext_credits_2'];
				$data['ext_credits_3']=$rule['ext_credits_3'];
				$data['is_read']=0;
				$logdb->update(array('event_log_id'=>$log['event_log_id']), $data);
				load_api('user')->updateCredits($rule, $userid);
				return;
			}else if(($rule['cycletype']=='notimelimit'&&$log['total']<$rule['rewardnum'])){
				$data['dateline']=TIMESTAMP;
				$data['cyclenum']=$log['cyclenum']+1;
				$data['total']=$log['total']+1;
				$data['ext_credits_1']=$rule['ext_credits_1'];
				$data['ext_credits_2']=$rule['ext_credits_2'];
				$data['ext_credits_3']=$rule['ext_credits_3'];
				$data['is_read']=0;
				$logdb->update(array('event_log_id'=>$log['event_log_id']), $data);
				load_api('user')->updateCredits($rule, $userid);
				return;
			}else if($rule['cycletype']=='everyday'){
				$now_date = date('Ymd',TIMESTAMP);
				$last_date = date('Ymd',$log['dateline']);
				if($now_date>$last_date){
					$data['starttime']=TIMESTAMP;
					$data['dateline']=TIMESTAMP;
					$data['cyclenum']=1;
					$data['total']=$log['total']+1;
					$data['ext_credits_1']=$rule['ext_credits_1'];
					$data['ext_credits_2']=$rule['ext_credits_2'];
					$data['ext_credits_3']=$rule['ext_credits_3'];
					$data['is_read']=0;
					$logdb->update(array('event_log_id'=>$log['event_log_id']), $data);
					load_api('user')->updateCredits($rule, $userid);
					return;
				}elseif ($now_date==$last_date&&$log['cyclenum']<$rule['rewardnum']){
					$data['dateline']=TIMESTAMP;
					$data['cyclenum']=$log['cyclenum']+1;
					$data['total']=$log['total']+1;
					$data['ext_credits_1']=$log['ext_credits_1']+$rule['ext_credits_1'];
					$data['ext_credits_2']=$log['ext_credits_2']+$rule['ext_credits_2'];
					$data['ext_credits_3']=$log['ext_credits_3']+$rule['ext_credits_3'];
					$data['is_read']=0;
					$logdb->update(array('event_log_id'=>$log['event_log_id']), $data);
					load_api('user')->updateCredits($rule, $userid);
					return;
				}
			}
		}
	}

	//array('daylogin','setavatar','postshare','postvideo',
	//			'postcomment','realemail','addlike','beenlike','digist',
	//			'top','follow','beenfollow','register','addlikealbum');


	private function setavatar_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'setavatar');
	}

	private function postshare_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'postshare',$title,$relid);
	}

	private function postvideo_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'postvideo',$title,$relid);
	}

	private function postcomment_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'postcomment',$title,$relid,$touid);
	}

	private function addlike_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'addlike',$title,$relid,$touid);
	}

	private function follow_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'follow',$title,$relid,$touid);
	}

	private function addlikealbum_p($uid,$title='',$relid=0,$touid=0){
		load_db('event/activity')->addActivity($uid,'addlikealbum',$title,$relid,$touid);
	}

	private function register_p($uid,$isocial=false){
		$emailapi = load_api('email');
		$regsetting = load_api('setting')->getSetting('register');
		if($regsetting['emailopen']&&!$isocial){
			log_message('debug',"event register");
			$emailapi->asyn_sendmail('register',$uid);
		}
		if($regsetting['verify_type']!='no'&&!$isocial){
			load_api('user')->updateUserSystemGroupVerify($uid);
			if($regsetting['verify_type']=='email'){
				$emailapi->asyn_sendmail('registerverify',$uid);
			}
		}else{
			load_api('user')->updateUserSystemGroupMember($uid);
		}
	}

	private function daylogin_p($uid){
		if($uid){
			$userdb = load_db('user');
			$userdb->update(array('user_id'=>$uid),array('last_login_time'=>TIMESTAMP));
		}
	}
	
	private function plugin_process($event,$args){
		load_api('mmg')->invokeEvent($event,$args);
	}
}
